<?php 
if(KT!='KhiThe') exit;
class systems_controller extends systems_model
{
	public $file_controller;
	public function __construct()
	{
		global $lang;

		$lang['url_base'] = url_base;	
		$this->path_cur = dirname(__FILE__);	
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_upload');
		
		/*--------------$route--------------------------------*/
		if(isset($_REQUEST['kt']))
		{
			$route = (empty($_REQUEST['kt'])) ? '' : $_REQUEST['kt'];
		}
		
		$this->parts = explode('/', $route);
        $parts1 = isset($this->parts[1])?$this->parts[1]:NULL;
		parent::addObject('khithe_captcha');
		if($parts1=='img')
		{			
			echo $this->showImage();
			exit();
		}
		if($this->parts[0]!='' && $parts1!='' && $parts1!='systems')
		{					
				define("path_com",path_app.$this->parts[0].'/com_'.$this->parts[1].'/',true);
				$this->file_lang = path_base.'/languages/'.$this->parts[0].'/'.dir_lang.'/'.$this->parts[1].'.php';
				$this->file_controller = path_com.$this->parts[1].'_controller.php';
				$this->file_model = path_com.$this->parts[1].'_model.php';
				$this->controller = $this->parts[1].'_controller';
				$this->model = $this->parts[1].'_model';
		}


		/*--------------Include Ojb--------------------------------*/
		if(is_file($this->file_controller) && is_file($this->file_model))
		{
			include_once($this->file_lang);
			include_once($this->file_model);
			include_once($this->file_controller);
			$this->ojb_controller = new $this->controller();
		}


        if(isset($_COOKIE['US']) && isset($_COOKIE['UID']) && isset($_COOKIE['PS'])){

            $_arrx['id'] = $this->de_id($_COOKIE['UID']);
            $_arrx['username'] = $_COOKIE['US'];
            $_arrx['password'] = $_COOKIE['PS'];

            $rowc=parent::getCookiesLogin($_arrx);
            if(!empty($rowc)){
                $_SESSION['ID']=$rowc['ID'];
                $_SESSION['Username']=$rowc['Username'];
                $_SESSION['Fullname']=$rowc['Fullname'];
                $_SESSION['Mod']=$rowc['Mod'];
            }
        }


        if(isset($_POST['username']) && $_POST['username']!="")
		{
			$row=parent::getLogin($_POST);
			$_SESSION['ID']=$row['ID'];
			$_SESSION['Username']=$row['Username'];
			$_SESSION['Fullname']=$row['Fullname'];
			$_SESSION['Mod']=$row['Mod'];


            $session_id = $this->en_id($_SESSION['ID']);
            setcookie ('UID', $session_id, time() + (24 * 3600));
            setcookie ('US', $_SESSION['Username'], time() + (24 * 3600));
            setcookie ('PS', $row['Password'], time() + (24 * 3600));

		}

		if(isset($_POST['out']) && $_POST['out']=="logout")
		{
			unset($_SESSION['ID']);
			unset($_SESSION['Username']);
			unset($_SESSION['Fullname']);
			unset($_SESSION['Mod']);
            setcookie ('UID', '1', time() - (24 * 3600));
            setcookie ('US', '1', time() - (24 * 3600));
            setcookie ('PS', '1', time() - (24 * 3600));
			session_destroy();
			//$this->gotoload(url_base);
		}

		if( isset($_SESSION['Mod']) && ($_SESSION['Mod']=='Admin' || $_SESSION['Mod']=='Members') ){
			
			if(empty($_SESSION['i']) && $_SESSION['Mod']=='Members')
			{
				$_SESSION['i'] = 1;
				$this->gotoload(url_base."admin/news/");
			}
			
			$lang['str_Login'] = '<form id="logout" name="logout" method="post"><input type="hidden" id="out" name="out" value="logout" /><a href="javascript:setLoguot()">'.$lang['Logout'].'</a></form>';
		}else{
			$lang['str_Login'] = '<a href="'.url_base.'admin/">'.$lang['Login'].'</a>';
		}		
		
		$this->lang = $lang;
		
		echo $this->ini_action();
	}
	
	public function ini_action()
	{
        $parts1 = isset($this->parts[1])?$this->parts[1]:NULL;
		if($this->parts[0]=='' || $parts1=='' || is_dir(path_app.$this->parts[0].'/com_'.$parts1.'/'))
		{
			if(isset($_REQUEST['ajax'])){
				$this->loadPage = $_REQUEST['ajax'];
				
			}else if(isset($_REQUEST['iframe'])){
				$this->loadPage = $_REQUEST['iframe']; 
			
			}else{
				$this->loadPage = "";
			}
			
			/*---------------Run Ojb-----------------------------------*/		
			switch ($this->loadPage)
			{
				case "get": /*ajax*/	
				case "post":
					if($_SESSION['Mod']=='Admin' || $_SESSION['Mod']=='Members')
					{ 
						if($this->parts[1]!='' && $this->parts[1]!='systems')
						{
							$htmlContent = $this->ojb_controller->index_ajax_action();
						}else{
							$htmlContent = $this->index_ajax_action();
						}
					}/*End Check Permission*/
				break;
				
				case "true": /*iframe*/
				default:
								
					if(isset($this->parts[1]) && $this->parts[1]!='' && $this->parts[1]!='systems')
					{
						if($_SESSION['Mod']=='Admin' || $_SESSION['Mod']=='Members')
						{ /*Check Permission*/
							
							if(isset($_REQUEST['iframe']) && $_REQUEST['iframe']=='true'){
									/*Run Com systems*/								
									$_action = $this->parts[2].'_action';
									try {
										$iframeCom = $this->ojb_controller->$_action();
									}catch (Exception $e) {
										echo "Not Find Action <br />".$e->getMessage();
										exit;
									}
																
									$strHtmlContent = $this->khithe_display_template(path_temp.'iframe');
									$htmlContent = $this->khithe_assign($strHtmlContent,array(
										'content_systems'=>$iframeCom,
									));
											
							} /*end iframe*/
							else{
									/*Run Component*/
									if(isset($this->parts[2]) && $this->parts[2])
									{
										$_action = $this->parts[2].'_action';
										
										try
										{
											$strHtmlCom = $this->ojb_controller->$_action();
										}
										catch (Exception $e) 
										{
											echo "Not Find Action <br />".$e->getMessage();
											exit;
										}
									}
									else{
										$strHtmlCom = $this->ojb_controller->index_action();
									}
									
									$strHtmlCom = $this->khithe_assign($strHtmlCom, $this->lang);
									
									if($_SESSION['Mod']=='Members')
									{
										$strHtmlContent = $this->khithe_display_template(path_temp.'mod');
									}
									else{ 
										$strHtmlContent = $this->khithe_display_template(path_temp.'index');
									}
									
									$htmlContent = $this->khithe_assign($strHtmlContent,array(
										'content_systems'=>$strHtmlCom,
									));
							}
							
						}else{
							$this->sms($this->lang["notIn"]);
							$this->gotoload(url_base.'admin/');
							 /* End Check Permission */ 
						}
					
					}else{
					
						$htmlContent = $this->index_action();
					}
					
				break;
			} /*switch ($ajax)*/
			
			$this->lang['jsCss']=$this->setJsCss($parts1);
			$strHtmlContent = $this->khithe_assign($htmlContent, $this->lang);
			
		}else{
			
			echo "Not find component";
		}
		
		return $strHtmlContent;
	}
		
	public function index_action()
	{
		$this->lang['sms'] ='';
		$resultHtml = ''; 		
		if( isset($_SESSION['Mod']) && ($_SESSION['Mod']=='Admin' || $_SESSION['Mod']=='Members') ){
		
			if( !empty($_POST['SettingConfig']))
			{
				parent::updateInsertSet($_POST['SettingConfig']);
				$this->gotoload(url_base.'admin/');
			}			
			
			$strHtmlCom = $this->khithe_display_template($this->path_cur.'/views/index');
			$arrHtml = $this->khithe_getBlockHtml($strHtmlCom);	
			
			$settings = parent::get_setting();
			if($settings)
			{
				while( $row = mysql_fetch_assoc($settings) )
				{
					$resultHtml .= $this->khithe_assign($arrHtml['set'], $row);					
				}							
				
			}/* if($settings) */

			$strHtmlContent = $this->khithe_assignBlock($strHtmlCom, array('htmlset'=>$resultHtml));
			$strHtmlCom = $this->khithe_assign($strHtmlContent, $this->lang);
			if($_SESSION['Mod']=='Members')
			{
				$strHtmlContent = $this->khithe_display_template(path_temp.'mod');
			}
			else{ 
				$strHtmlContent = $this->khithe_display_template(path_temp.'index');
			}
			
		}else{
			$strHtmlCom = $this->khithe_display_template($this->path_cur.'/views/login');
			$strHtmlCom = $this->khithe_assign($strHtmlCom, $this->lang);
			$strHtmlContent = $this->khithe_display_template(path_temp.'account');
		}
		
		
		$htmlContent = $this->khithe_assign($strHtmlContent,array('content_systems'=>$strHtmlCom));
			
		$strHtmlContent = $this->khithe_assign($htmlContent, $this->lang);
		return $strHtmlContent;
	}
	
	public function index_ajax_action(){
		
		/*Run Com systems ajax*/
		$str_html_ajax = $this->khithe_display_template($this->path_cur.'/views/ajax');		
		$str_html_ajax = $this->khithe_assign($str_html_ajax, $this->lang);
		
		return $str_html_ajax;
	}
	
}
?>